package atm.frame;

import atm.ATMGUIMain;
import atm.pojo.UserDO;
import atm.service.Impl.UserServiceImpl;
import atm.service.UserService;
import atm.util.DBUtil;
import atm.util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

//修改密码界面
public class ChangePwdPanel extends JPanel implements ActionListener {
    private Icon changePwdImage = Util.getIcon("/image/buttonModify.jpg");//改密按钮图
    private Icon backImage = Util.getIcon("/image/pane.jpg");//返回按钮图
    private ATMGUIMain frame;//顶级容器实例
    private List<UserDO> usersList;//用户信息集合实例
    private JButton backBtn;//返回按钮
    private JLabel jLabelBack;//返回文本
    private JLabel oldPwdJLabel;//旧密码提示插件
    private JTextField iptOldPwd;//旧密码输入框
    private JLabel newPwdJLabel;//新密码提示插件
    private JTextField iptNewPwd;//新密码输入框
    private JLabel confirmPwdJLabel;//确认新密码提示插件
    private JTextField iptConfirmPwd;//确认新密码输入框
    private JButton changePwdBtn;//改密按钮
    private Font font = new Font("宋体", Font.PLAIN, 20);
    private UserService userService = new UserServiceImpl();
    public ChangePwdPanel(ATMGUIMain frame) {
        this.frame = frame;
        //设置空布局和透明度
        this.setOpaque(false);
        this.setLayout(null);
        //旧密码提示插件
        oldPwdJLabel = new JLabel("请输入旧密码");
        oldPwdJLabel.setBounds(240, 190, 150, 50);
        oldPwdJLabel.setFont(font);
        this.add(oldPwdJLabel);
        //旧密码输入框
        iptOldPwd = new JTextField();
        iptOldPwd.setBounds(420, 200, 300, 30);
        iptOldPwd.setFont(font);
        this.add(iptOldPwd);
        //新密码提示插件
        newPwdJLabel = new JLabel("请输入新密码");
        newPwdJLabel.setBounds(240, 230, 150, 50);
        newPwdJLabel.setFont(font);
        this.add(newPwdJLabel);
        //新密码输入框
        iptNewPwd = new JTextField();
        iptNewPwd.setBounds(420, 240, 300, 30);
        iptNewPwd.setFont(font);
        this.add(iptNewPwd);
        //确认新密码提示插件
        confirmPwdJLabel = new JLabel("请再次输入新密码");
        confirmPwdJLabel.setBounds(240, 270, 200, 50);
        confirmPwdJLabel.setFont(font);
        this.add(confirmPwdJLabel);
        //确认新密码输入框
        iptConfirmPwd = new JTextField();
        iptConfirmPwd.setBounds(420, 280, 300, 30);
        iptConfirmPwd.setFont(font);
        this.add(iptConfirmPwd);
        //改密码按钮
        changePwdBtn = new JButton(changePwdImage);
        changePwdBtn.setBounds(870, 220, 140, 50);
        this.add(changePwdBtn);
        changePwdBtn.addActionListener(this);

        //返回按钮
        jLabelBack = new JLabel("返  回");
        jLabelBack.setBounds(900, 300, 100, 50);
        jLabelBack.setForeground(Color.white);
        this.add(jLabelBack);
        jLabelBack.setFont(new Font("宋体", Font.BOLD, 25));
        backBtn = new JButton(backImage);
        backBtn.setBounds(870, 300, 140, 50);
        backBtn.setOpaque(false);
        this.add(backBtn);
        backBtn.addActionListener(this);//建立联系
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == backBtn) {
            iptOldPwd.setText("");
            iptNewPwd.setText("");
            iptConfirmPwd.setText("");
            frame.switchPanel("menu");
        } else if (e.getSource() == changePwdBtn) {
            try {
                userService.changePassword(iptOldPwd.getText(),iptNewPwd.getText(),frame.getLoginUser().getId());
                JOptionPane.showMessageDialog(this, "修改密码成功，请重新登录，新密码为：" + iptConfirmPwd.getText());
                iptOldPwd.setText("");
                iptNewPwd.setText("");
                iptConfirmPwd.setText("");
                frame.getLoginPanel().getIptPassword().setText("");
                frame.switchPanel("login");
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this,exception.getMessage());
            }
        }
    }
}
